//
//  YYFileManager.m
//  XinMi_Java
//
//  Created by 蓓蕾 on 2022/4/25.
//

#import "YYFileManager.h"

@implementation YYFileManager



#pragma mark - 获取Home目录路径
+(NSString *)getHomeDirectory {
    NSString *homeDirectory = NSHomeDirectory();
    return homeDirectory;
}

#pragma mark - 获取Temp目录路径
+(NSString *)getTempDirectory {
    NSString *tempPath = NSTemporaryDirectory();
    return tempPath;
}

#pragma mark - 获取Documents目录路径
+(NSString *)getDocumentsDirectory {
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    return documentsPath;
}

#pragma mark - 获取Library目录路径
+(NSString *)getLibraryDirectory {
    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    return libraryPath;
}

#pragma mark - 获取Cache目录路径
+(NSString *)getCacheDirectory {
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    return cachePath;
}
#pragma mark - 追加文件或目录
+(NSString *)appendingPathComponent:(NSString *)path withAppendingPath:(NSString *)appendingPath {
    NSString *resultPath = [path stringByAppendingPathComponent:appendingPath];
    return resultPath;
}

#pragma mark - 获取路径的组成部分
+(NSArray *)getPathComponents:(NSString *)path {
    NSArray *pathComponents = [path pathComponents];
    return pathComponents;
}

#pragma mark - 获取路径的最后一个组成部分
+(NSString *)getLastPathComponents:(NSString *)path {
    NSString *lastPathComponents = [path lastPathComponent];
    return lastPathComponents;
}

#pragma mark - 删除路径最后的一个组成部分
+(NSString *)deleteLastPathComponents:(NSString *)path {
    return [path stringByDeletingLastPathComponent];
}

#pragma mark - 获取路径最后部分的扩展名
+(NSString *)getPathExtension:(NSString *)path {
    return [path pathExtension];
}

#pragma mark - 删除路径的扩展名
+(NSString *)deletePathExtension:(NSString *)path {
    return [path stringByDeletingPathExtension];
}

#pragma mark - 在路径后追加扩展名
+(NSString *)appendingPathExtension:(NSString *)path withExtension:(NSString *)extension {
    return [path stringByAppendingPathExtension:extension];
}

/// 将字典保存到指定路径下
/// @param dictionary 需要保存的字典
/// @param filePath 需要保存的路径【包含文件名称：/Users/beilei/Library/Developer/CoreSimulator/Devices/Library/User_87/name.plist】
+(BOOL)saveDictionaryToFile:(NSDictionary *)dictionary WithFilePath:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    NSString *fileFolderPath = [filePath stringByDeletingLastPathComponent];
    BOOL isExistFolder= [fileManager fileExistsAtPath:fileFolderPath isDirectory:&isDir];
    if (!isExistFolder && !isDir) {
        if (![fileManager createDirectoryAtPath:fileFolderPath withIntermediateDirectories:YES attributes:@{} error:nil]) {
            NSLog(@"文件夹创建失败");
        }
    }
    if (![dictionary writeToFile:filePath atomically:YES]) {
        NSLog(@"添加文件失败");
        return NO;
    } else {
        return YES;
    }
}
#pragma mark - 将数组保存到本地
+(BOOL)saveArrayToFile:(NSArray *)array WithFilePath:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    NSString *fileFolderPath = [filePath stringByDeletingLastPathComponent];
    BOOL isExistFolder= [fileManager fileExistsAtPath:fileFolderPath isDirectory:&isDir];
    if (!isExistFolder && !isDir) {
        if (![fileManager createDirectoryAtPath:fileFolderPath withIntermediateDirectories:YES attributes:@{} error:nil]) {
            NSLog(@"文件夹创建失败");
        }
    }
    if (![array writeToFile:filePath atomically:YES]) {
        NSLog(@"添加文件失败");
        return NO;
    } else {
        return YES;
    }
}
#pragma mark - 将对象保存至本地
+(BOOL)saveObjectToFile:(id)dictionary WithFilePath:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    NSString *fileFolderPath = [filePath stringByDeletingLastPathComponent];
    BOOL isExistFolder= [fileManager fileExistsAtPath:fileFolderPath isDirectory:&isDir];
    if (!isExistFolder && !isDir) {
        if (![fileManager createDirectoryAtPath:fileFolderPath withIntermediateDirectories:YES attributes:@{} error:nil]) {
            NSLog(@"文件夹创建失败");
        }
    }
    if (![dictionary writeToFile:filePath atomically:YES]) {
        NSLog(@"添加文件失败");
        return NO;
    } else {
        return YES;
    }
}
#pragma mark - 从指定路径下获取一个字典对象
+(NSDictionary *)getDictionaryFromFilePath:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    BOOL isExistFile= [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
    if (isDir) {
        NSLog(@"是文件夹");
    } else {
        NSLog(@"是文件夹或者不存在");
    }
    if (isExistFile && !isDir) {
        NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:filePath];
        return dictionary;
    } else {
        return nil;
    }
}
#pragma mark - 从指定路径下获取一个数组对象
+(NSArray *)getArrayFromFilePath:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    BOOL isExistFile= [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
    if (isDir) {
        NSLog(@"是文件夹");
    } else {
        NSLog(@"是文件夹或者不存在");
    }
    if (isExistFile && !isDir) {
        NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
        return array;
    } else {
        return nil;
    }
}
#pragma mark - 从指定路径下获取一个对象
+(id)getObjectFromFilePath:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    BOOL isExistFile= [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
    if (isDir) {
        NSLog(@"是文件夹");
    } else {
        NSLog(@"是文件夹或者不存在");
    }
    if (isExistFile && !isDir) {
        NSData *data = [fileManager contentsAtPath:filePath];
        id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        return object;
    } else {
        return nil;
    }
}
#pragma mark - 删除指定文件
+(BOOL)deleteFileWithFilePath:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    BOOL isExistFile= [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
    if (isDir) {
        NSLog(@"是文件夹");
    } else {
        NSLog(@"是文件夹或者不存在");
    }
    if (isExistFile && !isDir) {
        if (![fileManager removeItemAtPath:filePath error:nil]) {
            NSLog(@"文件删除失败");
            return NO;
        } else {
            NSLog(@"文件删除成功");
            return YES;
        }
    } else {
        NSLog(@"文件不存在吧");
        return NO;
    }
}

+(BOOL)createFileWithFilePath:(NSString *)filePath{
    return YES;
}

+(NSString *)toJsonStrWithDictionary:(NSDictionary *)dict {
    if ([dict isKindOfClass:[NSString class]]) {
        return (NSString *)dict;
    }
    
    NSError *error = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (error) {
        jsonStr = @"";
    }
    return jsonStr;
}

+(NSString *)toJsonStrWithDictionary2:(NSDictionary *)dict {
    if ([dict isKindOfClass:[NSString class]]) {
        return (NSString *)dict;
    }
    
    NSError *error = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
    NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (error) {
        jsonStr = @"";
    }
    return jsonStr;
}

+(NSDictionary *)toDctionaryWithJsonString:(NSString *)jsonStr {
    if ([jsonStr isKindOfClass:[NSDictionary class]]) {
        return (NSDictionary *)jsonStr;
    }
    NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    return resultDic;
}

@end




///// 将字典保存到指定路径下
///// @param dictionary 需要保存的字典
///// @param filePath 需要保存的路径【包含文件名称：/Users/beilei/Library/Developer/CoreSimulator/Devices/Library/User_87/name.plist】
//+(BOOL)saveDictionaryToFile:(NSDictionary *)dictionary WithFilePath:(NSString *)filePath{
//    NSFileManager *fileManager = [NSFileManager defaultManager];
//    BOOL isDir = NO;
//    NSString *fileFolderPath = [filePath stringByDeletingLastPathComponent];
////    NSLog(@"carInfoFolderPath = %@",carInfoFolderPath);
//    BOOL isExistFolder= [fileManager fileExistsAtPath:fileFolderPath isDirectory:&isDir];
////    if (isDir) {
////        NSLog(@"是文件夹");
////    } else {
////        NSLog(@"是文件或者不存在");
////    }
//    if (!isExistFolder && !isDir) {
//        if (![fileManager createDirectoryAtPath:fileFolderPath withIntermediateDirectories:YES attributes:@{} error:nil]) {
//            NSLog(@"文件夹创建失败");
//        }
////        else {
////            NSLog(@"文件夹创建成功");
////        }
//    }
////    if (isExistFile && !isDir) {
////        carInfoDic = [NSDictionary dictionaryWithContentsOfFile:carInfoPath];
////    }
////    NSMutableArray *TEMPARR = [NSMutableArray arrayWithArray:BILLLISTARR];\
////    [TEMPARR addObject:billDic];
//    if (![dictionary writeToFile:filePath atomically:YES]) {
//        NSLog(@"添加车辆信息失败");
//    }
//    return YES;
//}
